//
//  BaiduMapSdkManager.m
//  YuanXinBaiduMap
//
//  Created by GJS on 2022/2/10.
//  Copyright © 2022 晏德智. All rights reserved.
//

#import "BaiduMapSdkManager.h"
#import <BMKLocationkit/BMKLocationComponent.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>

static NSString * AuthKey = @"";

@interface BaiduMapSdkManager ()<BMKLocationAuthDelegate>
@property (nonatomic, strong) BMKMapManager *mapManager; //主引擎类
@end

@implementation BaiduMapSdkManager

+ (instancetype)shareInstance {
    static BaiduMapSdkManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    
    return _manager;
}

+ (void)initSDK:(NSString*)key {
    AuthKey = key;
    [[self shareInstance] checkPermision];
}

- (void)checkPermision {
    
    // 初始化定位SDK
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:AuthKey authDelegate:self];
    //要使用百度地图，请先启动BMKMapManager//主引擎类
    if (_mapManager == nil) {
        _mapManager = [[BMKMapManager alloc] init];
    }
    
    /**
     百度地图SDK所有API均支持百度坐标（BD09）和国测局坐标（GCJ02），用此方法设置您使用的坐标类型.
     默认是BD09（BMK_COORDTYPE_BD09LL）坐标.
     如果需要使用GCJ02坐标，需要设置CoordinateType为：BMK_COORDTYPE_COMMON.
     */
    if ([BMKMapManager setCoordinateTypeUsedInBaiduMapSDK:BMK_COORDTYPE_BD09LL]) {
        NSLog(@"经纬度类型设置成功");
    } else {
        NSLog(@"经纬度类型设置失败");
    }
    
    //启动引擎并设置AK并设置delegate
    BOOL result = [_mapManager start:AuthKey generalDelegate:self];
    if (!result) {
        NSLog(@"启动引擎失败");
    }
}

/**
 联网结果回调

 @param iError 联网结果错误码信息，0代表联网成功
 */
- (void)onGetNetworkState:(int)iError {
    if (0 == iError) {
        NSLog(@"联网成功");
    } else {
        NSLog(@"联网失败：%d", iError);
    }
}

/**
 鉴权结果回调

 @param iError 鉴权结果错误码信息，0代表鉴权成功
 */
- (void)onGetPermissionState:(int)iError {
    if (0 == iError) {
        NSLog(@"授权成功");
    } else {
        NSLog(@"授权失败：%d", iError);
    }
}

/**
 *@brief 返回授权验证错误
 *@param iError 错误号 : 为0时验证通过，具体参考BMKLocationAuthErrorCode
 */
- (void)onCheckPermissionState:(BMKLocationAuthErrorCode)iError{
    if (0 == iError) {
        NSLog(@"验证通过");
    } else {
        NSLog(@"验证失败：%ld", (long)iError);
    }
}

@end
